package com.rebecca.demo_01.controller;

import com.rebecca.demo_01.bean.User;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import java.util.*;

import static com.rebecca.demo_01.config.Constants.*;

@RestController
@RequestMapping(value = "/user")  // 通过这里的配置就可以使下面的配置都在/users 下
@Api(value = "用户信息文档API")
public class UserController {

    static Map<String, User> users = Collections.synchronizedMap(new HashMap<String, User>());
    /**
     * 处理"/users/"的GET请求，用来获取用户列表
     *
     * @return
     */
    @GetMapping("/")
    @ApiOperation(
            value = "获取用户列表"
            , tags = INTERFACE_GROUP_USER
            , httpMethod = "GET"
            , produces = "application/json"
            , protocols = "http"
    )
    public List<User> getUserList() {
        // 还可以通过@RequestParam从页面中传递参数来进行查询条件或者翻页信息的传递
        List<User> r = new ArrayList<User>(users.values());
        return r;
    }

    /**
     * 处理"/users/"的POST请求，用来创建User
     *
     * @param user
     * @return
     */
    @PostMapping("/")
    @ApiOperation(
            value = "创建User用户信息"
            ,tags = INTERFACE_GROUP_USER
            , httpMethod = "POST"
            , produces = "application/json"
            , protocols = "http"
    )
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户 id", required = true, paramType = "query", dataType = "String")
            , @ApiImplicitParam(name = "name", value = "用户名称", paramType = "query", dataType = "String")
    })
    public String postUser(@RequestBody User user) {
        // @RequestBody注解用来绑定通过http请求中application/json类型上传的数据
        // 请求时，请求头需要加入Content-type:application/json 参数
        users.put(user.getId(), user);
        return "success";
    }


    /**
     * 处理"/users/{id}"的GET请求，用来获取url中id值的User信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(
            value = "根据用户 ID 获取用户信息"
            , tags = INTERFACE_GROUP_USER
            , httpMethod = "GET"
            , produces = "application/json"
            , protocols = "http"
    )
    public User getUser(@PathVariable String id) {
        // url中的id可通过@PathVariable绑定到函数的参数中
        return users.get(id);
    }

    /**
     * 处理"/users/{id}"的PUT请求，用来更新User信息
     *
     * @param id
     * @param user
     * @return
     */
    @PutMapping("/{id}")
    @ApiOperation(
            value = "根据id更新用户信息"
            ,tags = INTERFACE_GROUP_USER
            , httpMethod = "PUT"
            , produces = "application/json"
            , protocols = "http"
    )
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户 id", required = true, paramType = "query", dataType = "String")
            , @ApiImplicitParam(name = "name", value = "用户名称", paramType = "query", dataType = "String")
    })
    public String putUser(@PathVariable String id, @RequestBody User user) {
        User u = users.get(id);
        u.setName(user.getName());
        users.put(id, u);
        return "success";
    }

    /**
     * 处理"/users/{id}"的DELETE请求，用来删除User
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(
            value = "根据id删除某用户信息"
            ,tags = INTERFACE_GROUP_USER
            , httpMethod = "DELETE"
            , produces = "application/json"
            , protocols = "http"
    )
    public String deleteUser(@PathVariable String id) {
        users.remove(id);
        return "success";
    }

}
